package game.module.announce.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.module.announce.bean.AnnounceBean;

/**
 * 设置缓存
 * 
 * @author zhangning
 * 
 * @Date 2015年5月29日 下午5:14:52
 */
public class AnnounceCache {

	private static Logger logger = LoggerFactory.getLogger(AnnounceCache.class);

	static class SingletonHolder {
		static AnnounceCache instance = new AnnounceCache();
	}

	public static AnnounceCache getInstance() {
		return SingletonHolder.instance;
	}

	/**
	 * 设置缓存<br/>
	 * Key：玩家唯一ID
	 */
	private Map<Integer, AnnounceBean> settingCacheAll = new HashMap<Integer, AnnounceBean>();

	/**
	 * 初始化数据到缓存中
	 * 
	 * @param playerId
	 */
	public void loadFromDb() {
		if (settingCacheAll.size() > 0) {
			return;
		}
		// Load玩家秘密基地
		List<AnnounceBean> announceAll = AnnounceDao.getInstance().getAnnounceAll();
		if (announceAll != null && announceAll.size() > 0) {
			for (AnnounceBean announceBean : announceAll) {
				settingCacheAll.put(announceBean.getId(), announceBean);
			}
		}
	}

	/**
	 * 下线删除缓存
	 * 
	 * @param playerId
	 */
	public void remove(int playerId) {
		settingCacheAll.remove(playerId);
	}

	/**
	 * 添加设置
	 * 
	 * @param setting
	 */
	public void addBean(AnnounceBean setting) {
		if (setting != null) {
			settingCacheAll.put(setting.getId(), setting);
		}
	}

	/**
	 * 设置
	 * 
	 * @param playerId
	 * @return
	 */
	public AnnounceBean getAnnounce(int beanId) {
		return settingCacheAll.get(beanId);
	}

	public Collection<AnnounceBean> getAnnounceAll() {
		return settingCacheAll.values();
	}

}
